package com.joinway.framework.data.jdbc.repository

import groovy.json.JsonOutput
import groovy.sql.Sql

import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.Statement

import org.junit.Before
import org.junit.Test

import com.joinway.framework.bean.domain.Domain

class TableRepositoryTest {

	Sql sqlClient
	TableRepository repository
	DummyTableRepositoryDelegate repositoryd;
	
	@Before void init(){
		sqlClient = Sql.newInstance("jdbc:mysql://localhost:3306/testdb?autoReconnect=true&useUnicode=true&characterEncoding=utf-8", 'test', 'test123', 'com.mysql.jdbc.Driver');
		repository = new TableRepository(sqlClient)
		repositoryd = new DummyTableRepositoryDelegate(repo:repository)
	}
	
	@Test public void test1(){
		List list = repositoryd.find(TestDomain.class, "select * from test_domain")
		println list
	}
	
	@Test public void test2(){
//		println String.valueOf("").charAt(0)
		println ' ' as char
		List list = repository.find(SubTestDomain.class, "select * from test_domain")
		println list
	}
	
	@Test public void test3(){
		List list = repository.find(TestDomain2.class, "select * from test_domain")
		println list
	}

	@Test public void test4(){
		List list = repository.find(TestDomain.class, "select id, name unknownName from test_domain t")
		println list
	}

	@Test public void test5(){
		List list = repository.find("select * from test_domain")
		list.each {
			println it.id
			println it.name
		}
	}
	
	@Test public void test6(){
		Domain domain
		
		(domain) = repository.find("select * from test_domain")
//		domain.metaClass.getProperties().each {
//			MetaProperty prop = it
//			String propName = prop.getName()
//			
//			if(!"class".equals(propName)){
//				def value = prop.getProperty(propName)
//				println "name=$propName, value=$value"
//			}
////			println prop.getGetter().getName()
////			println prop.getInitialValue()
////			println prop.get
//		}
//		Assert.assertTrue(domain instanceof GroovyObject);
//		println domain.getValue("name")
	}
	
	@Test public void test7(){
		int count = repository.update("update test_domain set name = 'lee2' where id = 2")
		println count
	}
	
	@Test public void test8(){
		TestDomain domain = new TestDomain()
//		domain.setFlag3(' ' as char)
//		String cn = domain.getClass().getSimpleName()
//		println cn
		repository.save(domain)
		println domain
	}
	
	@Test public void test9(){
		TestDomain domain = new TestDomain()
		domain.name = 'abc'
		domain.setAge(36)
		domain.setFlag3(' ' as char)
		repository.save(domain)
		println domain
	}
	
	@Test public void test10(){
		TestDomain domain = repository.findOne(3, TestDomain.class)
		domain = domain ? domain : new TestDomain()
		domain.name = 'iscc'
		domain.setAge(36)
		repository.save(domain)
		println domain
	}
	
	@Test public void test11(){
		TestDomain domain = repository.findOne(1, TestDomain.class)
		println domain
		assert domain != null
		
		domain = repository.findOne(1000, TestDomain.class)
		println domain
		assert domain == null
		
	}
	
//	@Test public void test12(){
//		println repository.findByName(TestDomain.class, "aaa")
//	}
//	
//	@Test public void test13(){
//		def domain = repository.findByName("test_domain", "iscc")
//		['id', 'name', 'age', 'createDate', 'updateDate'].each {
//			println domain."$it"
//		}
//	}
//	
//	@Test public void test14(){
//		println repository.findByNameAndDiscountPrice(TestDomain.class, ["aaa", "12"])
//	}
//	
//	@Test public void test15(){
//		println repository.findByNameAndAge(TestDomain.class, "aaa", "36")
//	}
//	
//	@Test public void test16(){
//		println repository.findByAge(TestDomain.class, 36)
//	}

	@Test public void test17(){
		println repository.findById(TestDomain.class, 1)
	}
	
	@Test public void test18(){
		repository.find("select distinct name from test_domain").each {
			println it.name
		}
	}

	@Test public void test19(){
		println repository.sqlClient.rows("select distinct name from test_domain")
	}
	
	
	@Test public void test20(){
		println repository.sqlClient.rows("select * from test_domain")
	}
	
	@Test public void test21(){
		List list = repository.find(TestDomain.class, "select t.id, t.name unknownName from test_domain t")
		println list
	}

	@Test public void test22(){
		List list = repository.find(TestDomain.class, "select id, name as unknownName from test_domain")
		println list
	}
	
	@Test public void test23(){
		Domain domain = new Domain()
//		domain.metaClass.id = 47
		domain.metaClass.name = 'lee49'
		
//		println domain.getProperties().find {it.key.equals('id')}
		println repository.persist("test_domain", domain, 'id');
	}
	
	@Test public void test24(){
		TestDomain2 domain = new TestDomain2()
//		domain.metaClass.id = 47
		domain.name = 'aaa'
		
//		println domain.getProperties().find {it.key.equals('id')}
		println repository.persist(domain);
	}
	
	@Test public void test25(){
		Sql client = Sql.newInstance("jdbc:mysql://localhost:3306/jiayida?autoReconnect=true&useUnicode=true&characterEncoding=utf-8", 'jiayida', 'group123', 'com.mysql.jdbc.Driver');
		TableRepository repo = new TableRepository(client)

		TestEntity e = new TestEntity()
		def domain =  repo.find("select * from admin_user where id = 4")
		println domain.lastLoginTime
		println JsonOutput.prettyPrint(JsonOutput.toJson(domain))
		println new Date()
		e.lastLoginTime = Calendar.getInstance(TimeZone.getTimeZone("Asia/Shanghai")).getTime()
		println JsonOutput.prettyPrint(JsonOutput.toJson(e))
		println TimeZone.getDefault()
		println TimeZone.getAvailableIDs()
	}
	
	@Test public void test26(){
		Sql client = Sql.newInstance("jdbc:mysql://localhost:3306/jiayida?autoReconnect=true&useUnicode=true&characterEncoding=utf-8", 'jiayida', 'group123', 'com.mysql.jdbc.Driver');
		String sql = "select sex from login_user where user_id = 28"
		client.eachRow(sql){
			println it.sex
			println it.getClass()
//			it.metaClass.properties
		}
		
		TableRepository repo = new TableRepository(client)
		repo.find(sql);
		
//		ResultSet rs = client.execute
//		println rs.getInt(1)
//		println client.rows(sql)
////		println rs
//		if(rs.next()){
//			println rs.getInt(1)
//		}
	}
	
	@Test public void test27(){
		Class.forName("com.mysql.jdbc.Driver")
		String url = "jdbc:mysql://localhost:3306/jiayida?autoReconnect=true&useUnicode=true&characterEncoding=utf-8";
		Connection con = DriverManager.getConnection(url, "jiayida", "group123")
		Statement stmt = con.createStatement();
		String sql = "select sex from login_user where user_id = 28"
		ResultSet rs = stmt.executeQuery(sql)
		if(rs.next()){
			println rs.getInt(1) 
		}
	}
	
	@Test public void test28(){
		Sql client = Sql.newInstance("jdbc:mysql://localhost:3306/jiayida?autoReconnect=true&useUnicode=true&characterEncoding=utf-8", 'jiayida', 'group123', 'com.mysql.jdbc.Driver');
		TableRepository repo = new TableRepository(client)
		String sql = "select count(*) total from login_user where upper(user_name) = ?"
		def (domain) = repo.find(sql, "lee000")
		println domain.total
	}
	
	@Test public void test29(){
		int count = repository.delete("delete from person where age = 11")
		print count
	}
}
